package com.gx.smart.construction.ui.area.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.casic.gx.grpc.common.ComQuery;
import com.casic.gx.grpc.common.ComResp;
import com.gx.smart.base.BaseMVVMRefreshActivity;
import com.gx.smart.base.BaseViewModel;
import com.gx.smart.common.util.AppConfig;
import com.gx.smart.lib.http.api.ConstructionAppSysTenantProviderService;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.wisestone.constructionappgrpclib.grpc.systenant.ConstructionAppSysTenantDto;
import com.gx.wisestone.constructionappgrpclib.grpc.systenant.ConstructionAppSysTenantQueryResponse;

import java.util.List;

/**
 * @author xiaosy
 * @create 2020/8/7
 * @Describe
 **/
public class AreaViewModel extends BaseViewModel {
    public MutableLiveData<List<ConstructionAppSysTenantDto>> data = new MutableLiveData<>();

    public AreaViewModel(@NonNull Application application) {
        super(application);
    }

    public void SysTenantList(int currentPage) {
        ComQuery comQuery = ComQuery.newBuilder()
                .setPage(currentPage)
                .setPageSize(BaseMVVMRefreshActivity.PAGE_SIZE)
                .build();
        String sysTenantIds = SPUtils.getInstance().getString(AppConfig.USER_TENANT_IDS, "");
        ConstructionAppSysTenantProviderService.getInstance().querySysTenant(comQuery, sysTenantIds, new CallBack<ConstructionAppSysTenantQueryResponse>() {
            @Override
            public void callBack(ConstructionAppSysTenantQueryResponse result) {
                if (result == null) {
                    error.setValue(true);
                    ToastUtils.showLong("服务器超时");
                    return;
                }

                ComResp comResp = result.getComResp();
                if (comResp.getCode() == 100) {
                    data.setValue(result.getContentList());
                } else {
                    error.setValue(true);
                    handleServiceException(comResp);
                }
            }
        });

    }
}
